package com.das.mechanic_base.widget;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.app.X3Constent;
import com.das.mechanic_base.bean.create.SelectWorkBean;
import com.das.mechanic_base.bean.greendao.AloneServiceNewBean;
import com.das.mechanic_base.bean.main.DoneDetectionBean;
import com.das.mechanic_base.bean.main.HomeMainCarBean;
import com.das.mechanic_base.mapi.api.NetWorkHttp;
import com.das.mechanic_base.mapi.response.HttpCallBack;
import com.das.mechanic_base.mapi.schedulers.RxSchedulersHelper;
import com.das.mechanic_base.mvp.view.create.fragment.X3AddServiceFastSelectAdapter;
import com.das.mechanic_base.utils.X3StringUtils;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chensi on 2018/3/9.
 */

public class X3AloneFastSelectDialog extends Dialog implements X3AddServiceFastSelectAdapter.IOnSelectWorkList {
    private Point mScreenPoint = new Point();
    private Activity mRootActivity = null;

    private TextView tv_tip;
    private Button btn_cancel;
    private Button btn_affirm;
    RecyclerView rlv_item;

    private X3AddServiceFastSelectAdapter x3AddServiceFastSelectAdapter;

    private List<DoneDetectionBean> mList;

    private List<DoneDetectionBean> mOpList = new ArrayList<>();


    /**
     * 没有按钮的提示dialog
     */
    public X3AloneFastSelectDialog(Activity arg,List<DoneDetectionBean> mList) {
        super(arg, R.style.pointDialog);
        mRootActivity = arg;
        this.mList = mList;
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawable(new BitmapDrawable());
        this.setContentView(R.layout.x3_alone_fast_select_dialog);
        initDialogWindow();
        initView();
    }

    void initView() {
        rlv_item = findViewById(R.id.rlv_item);
        btn_affirm = findViewById(R.id.btn_affirm);
        btn_cancel = findViewById(R.id.btn_cancel);
        tv_tip = findViewById(R.id.tv_tip);


        rlv_item.setLayoutManager(new LinearLayoutManager(mRootActivity));
        x3AddServiceFastSelectAdapter = new X3AddServiceFastSelectAdapter(mRootActivity,mList);
        x3AddServiceFastSelectAdapter.setiOnSelectWorkList(this);
        rlv_item.setAdapter(x3AddServiceFastSelectAdapter);
        btn_affirm.setTag(false);
        btn_affirm.setOnClickListener(view -> {
            boolean enable = (boolean) btn_affirm.getTag();
           if (!enable){
               return;
           }
            if (x3AddServiceFastSelectAdapter != null){
                handleSelectData(x3AddServiceFastSelectAdapter.getmList());
            }
            dismiss();
        });

        btn_cancel.setOnClickListener(view -> {
            dismiss();
        });

        if (X3Constent.isChina()) {
            String content  = getContext().getString(R.string.alone_fast_select_tips);
            SpannableString contentSpan = new SpannableString(content);
            contentSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#707380")), 0, content.length() - 10, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            contentSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#313340")), content.length() - 10, content.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            tv_tip.setText(contentSpan);
        }

    }

    private void handleSelectData(List<DoneDetectionBean> mList){
        if (iBtnClick != null){
            iBtnClick.iBtnAffirmClick(mList);
        }
    }

    IBtnClick iBtnClick;

    public void setiBtnClick(IBtnClick iBtnClick) {
        this.iBtnClick = iBtnClick;
    }

    @Override
    public void iOnSelectWork(DoneDetectionBean bean, boolean isSelect) {
        if (isSelect){
            mOpList.add(bean);
        } else {
            mOpList.remove(bean);
        }
        okBtnStatus(mOpList);
    }

    private void okBtnStatus(List<DoneDetectionBean> mOpList){
        if (mOpList.size() > 0){
            btn_affirm.setTextColor(Color.parseColor("#0077FF"));
            btn_affirm.setTag(true);
        } else {
            btn_affirm.setTextColor(Color.parseColor("#666666"));
            btn_affirm.setTag(false);
        }
    }

    public interface IBtnClick {
        void iBtnAffirmClick(List<DoneDetectionBean> mList);
    }

    @SuppressWarnings("deprecation")
    private void initDialogWindow() {
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);

        WindowManager m = mRootActivity.getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用

        try {
            d.getSize(mScreenPoint);
        } catch (NoSuchMethodError ignore) {
            mScreenPoint.x = d.getWidth();
            mScreenPoint.y = d.getHeight();
        }

        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        p.width = (int) (mScreenPoint.x * 0.80);    // 宽度设置为屏幕的0.9
        dialogWindow.setAttributes(p);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        mOpList.clear();
    }
}

